<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/const/Pattern.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/const/Error.php';
?>
<SCRIPT type="text/javascript" src="/view/javascripts/form.js"></SCRIPT>
<?php
if($response->isParameter('person')) {
	$person = $response->getParameter('person');
	$login = $person->getLogin();
	$surname = $person->getSurname();
	$name = $person->getName();
	$patronymic = $person->getPatronymic();
} else {
	$login = '';
	$surname = '';
	$name = '';
	$patronymic = '';
}
?>
<DIV class="block">
<?php
if($response->isParameter('person')) {
?>
	<H1><A href="/person/index.html">Сотрудники университета</A> :: <?php echo $person->getFullName(true);?></H1>
<?php
} else {
?>
	<H1><A href="/person/index.html">Сотрудники университета</A> :: новый сотрудник</H1>
<?php
}
?>
	<FORM action="/person/update.html" method="post" id="edit">
<?php
if($response->isParameter('person')) {
?>
		<INPUT type="hidden" name="identity" value="<?php echo $person->getIdentity();?>">
<?php
}
?>
		<TABLE>
			<TR>
				<TD><LABEL for="username">имя пользователя:</LABEL></TD>
				<TD>
					<INPUT type="text" id="username" name="login"
							value="<?php echo $login;?>"
							pattern="<?php echo Pattern::LOGIN;?>"
							message="<?php echo Error::INCORRECT_PERSON_LOGIN;?>"
							maxlength="30">
				</TD>
			</TR>
			<TR>
				<TD><LABEL for="surname">фамилия:</LABEL></TD>
				<TD>
					<INPUT type="text" id="surname" name="surname"
							value="<?php echo $surname;?>"
							pattern="<?php echo Pattern::WORD;?>"
							message="<?php echo Error::INCORRECT_PERSON_SURNAME;?>"
							maxlength="30">
				</TD>
			</TR>
			<TR>
				<TD><LABEL for="name">имя:</LABEL></TD>
				<TD>
					<INPUT type="text" id="name" name="name"
							value="<?php echo $name;?>"
							pattern="<?php echo Pattern::WORD;?>"
							message="<?php echo Error::INCORRECT_PERSON_NAME;?>"
							maxlength="30">
				</TD>
			</TR>
			<TR>
				<TD><LABEL for="patronymic">отчество:</LABEL></TD>
				<TD>
					<INPUT type="text" id="patronymic" name="patronymic"
							value="<?php echo $patronymic;?>"
							pattern="<?php echo Pattern::OPTIONAL_WORD;?>"
							message="<?php echo Error::INCORRECT_PERSON_PATRONYMIC;?>"
							maxlength="30">
				</TD>
			</TR>
		</TABLE>
	</FORM>
</DIV>
<DIV class="block">
	<BUTTON type="button" onclick="submitForm('edit')">
		<IMG src="/view/img/button-save.png" alt="">
		<SPAN>сохранить</SPAN>
	</BUTTON>
	<BUTTON type="button" onclick="resetForm('edit')">
		<IMG src="/view/img/button-reset.png" alt="">
		<SPAN>сбросить</SPAN>
	</BUTTON>
<?php
if($response->isParameter('person')) {
?>
	<FORM action="/person/delete.html" method="post" class="inline">
		<INPUT type="hidden" name="identity" value="<?php echo $person->getIdentity();?>">
		<BUTTON type="submit">
			<IMG src="/view/img/button-delete.png" alt="">
			<SPAN>удалить</SPAN>
		</BUTTON>
	</FORM>
<?php
}
?>
	<FORM action="/person/index.html" method="get" class="inline">
		<BUTTON type="submit">
			<IMG src="/view/img/button-back.png" alt="">
			<SPAN>назад</SPAN>
		</BUTTON>
	</FORM>
</DIV>
<?php
if($response->isParameter('person')) {
	$posts = $person->getPosts();
	$freePosts = $response->getParameter('free-posts');
	$allRoles = $response->getParameter('roles');
	$roles = $person->getRoles();
	$roleIdentities = array();
	foreach($roles as $role) {
		$roleIdentities[] = $role->getIdentity();
	}
?>
<DIV class="block">
	<H1>Должности, занимаемые сотрудником</H1>
	<TABLE>
		<TR>
			<TH>Должность</TH>
			<TH>Размер ставки</TH>
			<TH>&nbsp;</TH>
		</TR>
<?php
	foreach($posts as $post) {
?>
		<TR>
			<TD><?php echo $post->getName();?></TD>
			<TD><?php echo $post->getRate();?></TD>
			<TD>
				<FORM action="/employee/delete.html" method="post">
					<INPUT type="hidden" name="identity"
							value="<?php echo $post->getIdentity();?>">
					<BUTTON type="submit">
						<IMG src="/view/img/button-delete.png" alt="">
						<SPAN>удалить</SPAN>
					</BUTTON>
				</FORM>
			</TD>
		</TR>
<?php
	}
?>
		<FORM action="/employee/add.html" method="post">
			<INPUT type="hidden" name="id_person"
					value="<?php echo $person->getIdentity();?>">
			<TR>
				<TD>
					<SELECT name="id_post">
<?php
	foreach($freePosts as $freePost) {
?>
						<OPTION value="<?php echo $freePost->getIdentity();?>">
							<?php echo $freePost->getName();?>
						</OPTION>
<?php
	}
?>
					</SELECT>
				</TD>
				<TD>
					<SELECT name="rate">
						<OPTION value="1">1</OPTION>
						<OPTION value="0.75">0,75</OPTION>
						<OPTION value="0.5">0,5</OPTION>
						<OPTION value="0.25">0,25</OPTION>
					</SELECT>
				</TD>
				<TD>
					<BUTTON type="submit">
						<IMG src="/view/img/button-add.png" alt="">
						<SPAN>добавить</SPAN>
					</BUTTON>
				</TD>
			</TR>
		</FORM>
	</TABLE>
</DIV>
<DIV class="block">
	<H1>Смена пароля пользователя</H1>
	<FORM action="/person/password/set.html" method="post">
		<INPUT type="hidden" name="identity"
				value="<?php echo $person->getIdentity();?>">
		<TABLE class="inline">
			<TR>
				<TD>новый пароль:</TD>
				<TD><INPUT type="password" name="password"></TD>
				<TD>
					<BUTTON type="submit">
						<IMG src="/view/img/button-save.png" alt="">
						<SPAN>задать</SPAN>
					</BUTTON>
				</TD>
			</TR>
		</TABLE>
	</FORM>
</DIV>
<DIV class="block">
	<H1>Роли пользователя</H1>
	<TABLE>
		<TR>
			<TH>&nbsp;</TH>
			<TH>роль</TH>
		</TR>
<?php
	foreach($allRoles as $role) {
		$roleIdentiy = $role->getIdentity();
		$formId = 'role-'.$roleIdentiy;
?>
		<TR onclick="submitForm('<?php echo $formId;?>')">
			<TD>
				<FORM action="/person/role/switch.html" method="post"
						id="<?php echo $formId;?>">
					<INPUT type="hidden" name="identity"
							value="<?php echo $person->getIdentity();?>">
					<INPUT type="hidden" name="role"
							value="<?php echo $roleIdentiy;?>">
				</FORM>
<?php
		if(in_array($roleIdentiy, $roleIdentities, true)) {
?>
				<IMG src="/view/img/check.png" alt="есть">
<?php
		} else {
?>
				&nbsp;
<?php
		}
?>
			</TD>
			<TD><?php echo $role->getName();?></TD>
		</TR>
<?php
	}
?>
	</TABLE>
</DIV>
<?php
}
?>